package com.twu.biblioteca.menu;

import com.twu.biblioteca.Index;
import com.twu.biblioteca.Movie;
import com.twu.biblioteca.user.User;

import java.io.IOException;

public class MovieListAuthorized extends MovieList {
    private static final String PROMPT = "Enter a movie ID to view details.";

    public MovieListAuthorized(String title, User user, Index index, MenuInputReader reader) {
        super(title, user, index, reader);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(title + "\n");
        for(Movie movie : index.getMovies())
        {
            sb.append(movie.getItemId() + "\t" + movie.getTitle() + "\n");
        }
        sb.append("\n" + PROMPT + "\n");
        return sb.toString();
    }

    @Override
    public void run() {
        try
        {
            int movieId = reader.readNumber();
            Movie movie = index.getMovie(movieId);
            if(movie != null)
            {
                showMovieDetails(movie);
            }
        }
        catch(InvalidOptionException e)
        {
            System.out.println(e.getMessage());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private void showMovieDetails(Movie movie) {
        MenuItem movieDetails = new MovieDetails(movie.getTitle(), user, reader, movie);
        System.out.println(movieDetails.toString());
        movieDetails.run();
    }
}
